package com.sendpo.zjx.sendpo.services;

import android.content.Context;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * Created by 朱俊鑫 on 2017/5/12.
 * 高德地图定位服务
 */

public class AmapLocationService {
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener =null;
    public AMapLocationClientOption mLocationOption = null;
    private Context context;
   public AmapLocationService(Context context){
        this.context=context;
        if(mLocationClient==null){
            mLocationClient=new AMapLocationClient(context);
            mLocationClient.setLocationOption(getDefaultAmapLocationOptions());
        }

   }
   public void setmLocationOption(AMapLocationClientOption aMapLocationClientOption){
         if(mLocationClient!=null){
             mLocationClient.setLocationOption(aMapLocationClientOption);
         }
   }


    public AMapLocationClientOption getDefaultAmapLocationOptions(){
        if(mLocationOption==null){
            mLocationOption = new AMapLocationClientOption();
            //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            mLocationOption.setLocationCacheEnable(true);
            mLocationOption.setOnceLocation(true);
            mLocationOption.setOnceLocationLatest(false);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否强制刷新WIFI，默认为true，强制刷新。
            mLocationOption.setWifiScan(true);
            //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
            mLocationOption.setHttpTimeOut(20000);
        }
        return  mLocationOption;
    }

    public boolean registerListener(AMapLocationListener listener){
        boolean isSuccess=false;
           if(listener!=null){
               mLocationClient.setLocationListener(listener);
           }
        return true;
    }

    public boolean unregisterListener(AMapLocationListener listener){
        boolean isSuccess=false;
        if(listener!=null){
            mLocationClient.unRegisterLocationListener(listener);
        }
        return true;
    }


    public AMapLocationListener getmLocationListener() {
        return mLocationListener;
    }

    public AMapLocationClient getmLocationClient() {
        return mLocationClient;
    }

    public AMapLocationClientOption getmLocationOption() {
        return mLocationOption;
    }

    public void start(){
          if(mLocationClient.isStarted()!=true&&mLocationClient!=null){
              mLocationClient.startLocation();
          }
    }
    public void stop(){
        if(mLocationClient.isStarted()==true&&mLocationClient!=null){
            mLocationClient.stopLocation();
        }
    }
}
